//
//  DrawUtils.m
//  Ikinloop-demo
//
//  Created by Li Shen on 2021/4/22.
//  Copyright © 2021 lyh. All rights reserved.
//

#import "DrawUtils.h"
#import <QuartzCore/QuartzCore.h>

@interface DrawUtils()

@property (nonatomic, strong) CALayer *layer;
@property (nonatomic, assign) CGContextRef context;

@end
@implementation DrawUtils

- (instancetype)initWithLayer:(CALayer *)layer
{
    self = [super init];
    if (self) {
        self.layer  = layer;
        [self setupDefaultValues];
    }
    return self;
}

- (instancetype)initWithContext:(CGContextRef)context{
    self = [super init];
    if (self) {
        self.context  = context;
        [self setupDefaultValues];
    }
    return self;
}

- (instancetype)initWithLayer:(CALayer*)layer context:(CGContextRef)context{
    self = [super init];
    if (self) {
        self.layer = layer;
        self.context  = context;
        [self setupDefaultValues];
    }
    return self;
}

- (void)setupDefaultValues{
    self.drawerColor = [UIColor greenColor];
    self.bezierpath = [[UIBezierPath alloc] init];
    self.shapelayer = [[CAShapeLayer alloc] init];
}

- (void)setCGContextRef:(CGContextRef)context{
    self.context = context;
}

- (void)drawLineLineToPoint:(CGPoint)endPoint withLineWidth:(CGFloat)lineWidth{

    //画折线只能用一个bezierpath对象，所以这个对象在初始化的时候创建了，而且是用全局。
    [self addLineToBezierpath:self.bezierpath toEndPoint:endPoint withLineWidth:lineWidth];
    //同上，只能用一个shapelayer对象
    [self addShapelayer:self.shapelayer withBezierpath:self.bezierpath withColor:self.drawerColor];
}


- (void)beginBezierpath:(UIBezierPath*)bezierpath color:(UIColor*)color lineWidth:(CGFloat)lineWidth lineCap:(CGLineCap)lineCap lineJoin:(CGLineJoin)lineJoin{
    bezierpath.lineWidth = lineWidth;
    
}

//设置bezierpath的相关属性
- (void)addLineToBezierpath:(UIBezierPath *)bezierpath toEndPoint:(CGPoint)endPoint withLineWidth:(CGFloat)lineWidth{
    //贝塞尔线条的宽度
    bezierpath.lineWidth = lineWidth;
    //线条拐角：kCGLineCapRound为圆角
    bezierpath.lineCapStyle = kCGLineCapRound;
    //终点处理：kCGLineCapRound为圆角
    bezierpath.lineJoinStyle = kCGLineCapRound;
    //添加坐标点
    [bezierpath addLineToPoint:endPoint];
}

//绘制格子曲线
- (void)drawLineStartPoint:(CGPoint)startPoint toEndPoint:(CGPoint)endPoint withLineWidth:(CGFloat)lineWidth {
    
    //格子不能用一个bezierpath画，因为会首尾相连，所以要重复创建多个bezierpath对象
    UIBezierPath *bezierpath = [[UIBezierPath alloc] init];
    [bezierpath moveToPoint:startPoint];
    [self addLineToBezierpath:bezierpath toEndPoint:endPoint withLineWidth:lineWidth];
    //同上
    CAShapeLayer *shapelayer = [CAShapeLayer layer];
    [self addShapelayer:shapelayer withBezierpath:bezierpath withColor:self.drawerColor];
}

//设置shapelayer的相关属性
- (void)addShapelayer:(CAShapeLayer *)shapelayer withBezierpath:(UIBezierPath *)bezierpath withColor:(UIColor*)color{
    //CAShapeLayer 的背景色
    shapelayer.backgroundColor = [UIColor clearColor].CGColor;
    //CAShapeLayer填充色
    shapelayer.fillColor = [UIColor clearColor].CGColor;
    //线条拐角：kCGLineCapRound为圆角
    shapelayer.lineCap = kCALineCapRound;
    //终点处理：kCGLineCapRound为圆角
    shapelayer.lineJoin = kCALineJoinRound;
    //线条颜色：初始化时设为了绿色
    shapelayer.strokeColor = color.CGColor;
    //CAShapeLayer的线条宽度
    shapelayer.lineWidth = bezierpath.lineWidth;
    //CAShapeLayer添加到self.view上
    [self.layer addSublayer:shapelayer];
    //CAShapeLayer的path
    shapelayer.path = bezierpath.CGPath;
}

- (void)addBezierpath:(CAShapeLayer *)shapelayer withBezierpath:(UIBezierPath *)bezierpath withColor:(UIColor*)color{
    //CAShapeLayer 的背景色
    shapelayer.backgroundColor = [UIColor clearColor].CGColor;
    //CAShapeLayer填充色
    shapelayer.fillColor = [UIColor clearColor].CGColor;
    //线条拐角：kCGLineCapRound为圆角
    shapelayer.lineCap = kCALineCapRound;
    //终点处理：kCGLineCapRound为圆角
    shapelayer.lineJoin = kCALineJoinRound;
    //线条颜色：初始化时设为了绿色
    shapelayer.strokeColor = color.CGColor;
    //CAShapeLayer的线条宽度
    shapelayer.lineWidth = bezierpath.lineWidth;
    //CAShapeLayer的path
    shapelayer.path = bezierpath.CGPath;
}

- (void)clearRect:(CGFloat)x y:(CGFloat)y width:(CGFloat)width height:(CGFloat)height{
    CGContextClearRect(self.context, CGRectMake(x, y, width, height));
}

- (void)beginPath:(UIColor*)color lineWidth:(CGFloat)lineWidth lineCap:(CGLineCap)lineCap lineJoin:(CGLineJoin)lineJoin{
//    CGContextSetFillColorWithColor(self.context, [UIColor whiteColor].CGColor);
//    CGContextFillRect(self.context,self.layer.frame);
    
    
    CGContextSetLineWidth(self.context, lineWidth);
    CGContextSetLineCap(self.context, lineCap);
    CGContextSetLineJoin(self.context, lineJoin);
    CGContextSetStrokeColorWithColor(self.context, color.CGColor);
}

- (void)endPath{
    CGContextStrokePath(self.context);
}

- (void)drawLine:(CGPoint)start end:(CGPoint)end{
    [self moveTo:start];
    [self lineTo:end];
}

- (void)moveTo:(CGPoint)point{
    CGContextMoveToPoint(self.context, point.x, point.y);
}

- (void)lineTo:(CGPoint)point{
    CGContextAddLineToPoint(self.context, point.x, point.y);
}

- (void)fill:(UIColor*)color{
    CGContextFillPath(self.context);
}

- (void)fillRect:(CGRect)rect color:(UIColor*)color{
//    CGContextSetFillColorWithColor(self.context, color.CGColor);
//    CGContextFillRect(self.context,rect);
}

- (void)clearRect:(CGRect)rect{
    CGContextClearRect(self.context, rect);
}

+(CGFloat) mm2pt:(CGFloat)mm{
    return mm * [self dpi] / 25.4  / [UIScreen mainScreen].scale;
}

+(CGFloat)pt2mm:(CGFloat)pt{
    return pt * [UIScreen mainScreen].scale / [self dpi]  * 24;
}

+(CGFloat) dpi {
    CGFloat scale = [UIScreen mainScreen].scale;
    CGFloat dpi  = 1.0;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        dpi = 132 * scale;
    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        if (scale >= 3) {
            dpi = 133.6667 * scale;
        }else{
            dpi = 163 * scale;
        }
    } else {
        dpi = 160 * scale;
    }
    return dpi;
}


@end
